import logging
from redis.exceptions import RedisError
from django.db import DatabaseError
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import exception_handler as drf_exception_handler

loger = logging.getLogger('django')


def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)
    if response is None and (isinstance(exc, RedisError) or isinstance(exc, DatabaseError)):
        loger.error('{}: {}'.format(exc.__str__(), context['view']))
        response = Response({'error':'数据库连接错误'}, status=status.HTTP_507_INSUFFICIENT_STORAGE)
    return response
